﻿// IPC Library
// Interprocess Communication Library
// Copyright(C)2008 Bill Tudor
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

namespace IPCLibrary
{
	/// <summary>
	/// Contains all native windows calls.
	/// </summary>
	internal static class NativeMethods
	{
		internal const int MAILSLOT_WAIT_FOREVER = -1;
		internal const int INVALID_HANDLE_VALUE = -1;
		internal const int MAX_MAILSLOT_SIZE = 424;
		internal const int FILE_READ_DATA = 1;
		internal const int FILE_WRITE_DATA = 2;
		internal const int FILE_SHARE_READ = 1;
		internal const int FILE_SHARE_WRITE = 2;  
		internal const int STANDARD_RIGHTS_WRITE = (READ_CONTROL);
		internal const int FILE_WRITE_ATTRIBUTES = (0x0100);
		internal const int FILE_WRITE_EA = (0x0010);
		internal const int FILE_APPEND_DATA = (0x0004);
		internal const int SYNCHRONIZE = (0x00100000);
		internal const int READ_CONTROL = (0x00020000);

		internal const int FILE_MAP_READ = 4;
		internal const int FILE_MAP_WRITE = 2;
		internal const int PAGE_READWRITE = 4;

		internal const int FILE_GENERIC_WRITE = (STANDARD_RIGHTS_WRITE |
								   FILE_WRITE_DATA |
								   FILE_WRITE_ATTRIBUTES |
								   FILE_WRITE_EA |
								   FILE_APPEND_DATA |
								   SYNCHRONIZE);

		internal const int OPEN_EXISTING = 3;
		internal const int FILE_ATTRIBUTE_NORMAL = 0x00000080;

		[DllImport("kernel32.dll", SetLastError = true)]
		internal static extern Int32 GetLastError();
		[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
		internal static extern MailslotSafeHandle CreateMailslot(string lpName, Int32 nMaxMessageSize, Int32 lReadTimeout, IntPtr lpSecurityAttributes);
		[DllImport("kernel32.dll", SetLastError = true)]
		[return: MarshalAs(UnmanagedType.Bool)]
		internal static extern bool GetMailslotInfo(MailslotSafeHandle hMailslot, ref Int32 lpMaxMessageSize, ref Int32 lpNextSize, ref Int32 lpMessageCount, ref Int32 lpReadTimeout);
		[DllImport("kernel32.dll", SetLastError = true)]
		[return: MarshalAs(UnmanagedType.Bool)]
		internal static extern bool WriteFile(SafeFileHandle fFile, Byte[] lpBuffer, Int32 nNumberOfBytesToWrite, out Int32 lpNumberOfBytesWritten, IntPtr lpOverlapped);
		[DllImport("kernel32.dll", SetLastError = true)]
		[return: MarshalAs(UnmanagedType.Bool)]
		internal static extern bool ReadFile(SafeHandle hFile, byte[] lpBuffer, Int32 nNumberOfBytesToRead, out Int32 lpNumberOfBytesRead, IntPtr lpOverlapped);
		[DllImport("kernel32.dll", SetLastError = true)]
		[return: MarshalAs(UnmanagedType.Bool)]
		internal static extern bool CloseHandle(IntPtr hObject);
		[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
		internal static extern SafeFileHandle CreateFile(string lpFileName, Int32 dwDesiredAccess, Int32 dwShareMode, IntPtr lpSecurityAttributes, Int32 dwCreationDisposition, Int32 dwFlagsAndAttributes, IntPtr hTemplateFile);
		[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
		internal static extern SafeFileHandle CreateFileMapping(SafeHandle hFile, IntPtr lpFileMappingAttributes, Int32 flProtect, Int32 dwMaximumSizeHigh, Int32 dwMaximumSizeLow, string lpName);
		[DllImport("kernel32.dll", SetLastError = true)]
		internal static extern SafeViewOfFileHandle MapViewOfFile(SafeFileHandle hFileMappingObject, Int32 dwDesiredAccess, Int32 dwFileOffsetHigh, Int32 dwFileOffsetLow, Int32 dwNumberOfBytesToMap);
		[DllImport("kernel32.dll", SetLastError = true)]
		[return: MarshalAs(UnmanagedType.Bool)]
		internal static extern bool UnmapViewOfFile(IntPtr lpBaseAddress);
		[DllImport("kernel32.dll", SetLastError = true)]
		[return: MarshalAs(UnmanagedType.Bool)]
		internal static extern bool GetFileSizeEx(SafeFileHandle hFile, out Int64 lpFileSize);
		[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
		internal static extern LoadLibrarySafeHandle LoadLibrary(string lpFileName);
		[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Ansi)]
		internal static extern IntPtr GetProcAddress(LoadLibrarySafeHandle hModule, string lpProcName);
		[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
		[return: MarshalAs(UnmanagedType.Bool)]
		internal static extern bool FreeLibrary(IntPtr hModule);

	}
}
